package com.example.ffmpegcpp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.TextView;

import com.example.ffmpegcpp.databinding.ActivityMainBinding;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'ffmpegcpp' library on application startup.
    static {
        System.loadLibrary("native-lib");
        //avcodec-58 avdevice-58 avfilter-7 avformat-58 avutil-56 postproc-55 swresample-3 swscale-5
        System.loadLibrary("avcodec-58");
        System.loadLibrary("avdevice-58");
        System.loadLibrary("avfilter-7");
        System.loadLibrary("avformat-58");
        System.loadLibrary("avutil-56");
        System.loadLibrary("postproc-55");
        System.loadLibrary("swresample-3");
        System.loadLibrary("swscale-5");
    }
    private SurfaceView surfaceView;
    private Button button;
    private SurfaceHolder mHolder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = findViewById(R.id.sfv_player);
        button = findViewById(R.id.bt_play);
        button.setOnClickListener(v->{
            //找到SD卡中的视频文件
            Log.d("FileCheck","wenjian:"+getFilesDir());
            copyAssetToFile(this, "test.mp4");
            File video = new File(getFilesDir(), "test.mp4");
            //子线程进行视频渲染
            if (video.exists()) {
                Log.d("FileCheck", "文件存在：" + video.getAbsolutePath());
            } else {
                Log.d("FileCheck", "文件不存在：" + video.getAbsolutePath());
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    native_start_play(video.getAbsolutePath(),mHolder.getSurface());
                }
            }).start();
        });
        initSurfaceHolder();
    }
    private void copyAssetToFile(Context context, String fileName) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStream inputStream = assetManager.open(fileName);
            File outFile = new File(context.getFilesDir(), fileName);
            FileOutputStream outputStream = new FileOutputStream(outFile);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            inputStream.close();
            outputStream.close();
            Log.d("FileCopy", "文件已复制到：" + outFile.getAbsolutePath());
        } catch (IOException e) {
            Log.e("FileCopy", "无法复制文件：", e);
        }
    }
    private void initSurfaceHolder() {
        mHolder = surfaceView.getHolder();
        mHolder.setFormat(PixelFormat.RGBA_8888);
    }

    /**
     * 调用NDK的视频渲染
     * @param path 播放的视频的路径
     * @param surface 要渲染的surface
     * @return
     */
    public native void native_start_play(String path, Surface surface);
}